/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package redezeitor;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;

/**
 *
 * @author Mauriverti
 */
public class Connector extends Thread {

    int port;
    int idRouter;
    Integer matrix[][];
    ArrayList<Integer> routerNames = new ArrayList<>();
    int matrixSize;
    int thisPos;
    ArrayList<Link> links = new ArrayList<>();

    Connector() {
        port = 0;
    }

    Connector(int id, int _port) {
        idRouter = id;
        port = _port;
    }

    public int getIdRouter() {
        return idRouter;
    }

    public void setIdRouter(int idRouter) {
        this.idRouter = idRouter;
    }

    @Override
    public void run() {
        
    }
    
    public void cliente() throws SocketException, IOException {
        
        String s = new String();
        s = matrix2String();
        
        for (int i = 0; i < links.size(); i++) {
            int PORTA = 8000 + links.get(i).getOtherID();
            
            DatagramSocket socketUDP = new DatagramSocket();
            InetAddress IPServidor = InetAddress.getByName("127.0.0.1");
            
            //System.out.println("s["+ this.idRouter +"] " + s);
            byte[] msgEnvio = s.getBytes();
            
            DatagramPacket datagrama = new DatagramPacket (msgEnvio, msgEnvio.length, IPServidor, PORTA);
            
            socketUDP.send(datagrama);
            socketUDP.close();
            //System.out.println("[" + this.idRouter + "]Socket Fechado");
        }
        
    }
    
    public String matrix2String() {
        String s = new String();
        
        s += this.idRouter + "," + this.thisPos;
        
        for (int i = 0; i < matrixSize; i++) {
            s += "," + matrix [thisPos][i] ;
        }
        
        s += ";";
        
        //System.out.println("-> " + s);
        
        return s;
    }

    public void setMatrix(ArrayList<Integer> ids) {

        routerNames = ids;

        matrixSize = ids.size();

        matrix = new Integer[matrixSize][matrixSize];

        for (int i = 0; i < matrixSize; i++) {        // seta a matriz com infinito
            for (int j = 0; j < matrixSize; j++) {
                matrix[i][j] = 9999;
            }
        }
        
        for (int i = 0; i < matrixSize; i++) {

            if (ids.get(i) == this.idRouter) {
                thisPos = i;
                for (int j = 0; j < matrixSize; j++) {
                    if (i==j) {
                        matrix[i][j] = 0;
                    }
                    else {
                        matrix[i][j] = findDistance(ids.get(j));
                    }
                }
            }
            
        }
        
        matrix2String();

        
    }
    
    public String showMatrix() {

        String s = new String();
        
        s = "T" + idRouter;

        for (Integer id : routerNames) {
            s += "\t" + id;
        }

        s += "\n";
        
        for (int i = 0; i < matrixSize; i++) {
            s += routerNames.get(i);
            for (int j = 0; j < matrixSize; j++) {
                s += "\t";
                if (matrix[i][j] == 9999) {
                    s += "∞";
                } else {
                    s += matrix[i][j];
                }
            }
            s += "\n";
        }
        return s;
    }

    public int findDistance (int id) {
        
        for (Link link : links) {
            if (link.getOtherID() == id) return link.getDistance();
        }
        return 9999;
    }
    
    public void addLink(int idOtherRouter, int thisIP, int otherIP, int thisMask, int otherMask, int distance) {

        Link l = new Link(this.idRouter,idOtherRouter, thisIP, otherIP, thisMask, otherMask, distance);

        links.add(l);

    }

    public String getLinks() {
        String s = new String();

        for (Link link : links) {
            s += "[" + link.getOtherID()+ "," + link.getDistance() + "]\t";
        }

        return s;
    }

    public void removeLink(int idRouter) {

        for (int i = 0; i < links.size(); i++) {
            if (links.get(i).getOtherID() == idRouter) {
                links.remove(i);
            }
        }
    }

    public void removeSpecificLink(int idRouter, int distance) {

        for (int i = 0; i < links.size(); i++) {
            if (links.get(i).getOtherID() == idRouter && links.get(i).getDistance() == distance) {
                links.remove(i);
            }
        }
    }
}
